<template>
  <div class="editPhone">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="手机号" prop="tel">
        <el-input type="number" v-model="ruleForm.tel" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {updateTel} from "@/api/set";
import {isValidPhoneNumber} from "@/utils/validate";
export default {
  name: "editPhone",
  props:{
    row:{
      type:Object,
      default:{}
    }
  },
  data() {
    var validateTel = (rule, value, callback) => {
        if (value =='') {
          callback(new Error('请输入手机号'));
        } else {
          if (isValidPhoneNumber(value)) {
            callback();
          }else{
            callback(new Error('手机号格式错误'));
          }
        }
      };
    return {
      ruleForm: {
        tel: "",
      },
      rules: {
        tel: [{ required: true,validator: validateTel, trigger: "blur" }],
      }
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
        if(this.row.tel){
            this.ruleForm.phone = this.row.tel;
        }
        this.ruleForm.admin_id = this.row.user_id;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let result = await updateTel(this.ruleForm);
          if(result.code == 200){
            this.$message({
              type: "success",
              message: "添加成功!"
            });
            setTimeout(()=>{
                this.$emit("close");
            },500)
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>
.editPhone {
}
</style>
